package com.itheima.reggie.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.dto.OrderDto;
import com.itheima.reggie.entity.dto.OrdersDto;
import com.itheima.reggie.entity.vo.OrdersVo;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.utils.ThreadLocalUtil;
import com.itheima.reggie.web.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单控制器
 *
 * @Name : OrdersController
 * @Author : WuQiang
 * @Date : 2022-08-20 20:27
 * @Description : 订单控制器
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 下单结算
     *
     * @param ordersDto 请求参数
     * @return
     */
    @PostMapping("/submit")
    public R submit(@RequestBody OrdersDto ordersDto) {
        //保存订单和订单明细
        ordersService.sumitWithOrderDetail(ordersDto);
        return R.success("保存订单信息成功");
    }

    /**
     * 后台查询订单明细
     *
     * @param pageNum   当前页
     * @param pageSize  页面大小
     * @param number    订单号
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @GetMapping("/page")
    public R page(
            @RequestParam(name = "page", required = false, defaultValue = "1") Long pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10") Long pageSize,
            @RequestParam(name = "number", required = false, defaultValue = "") Long number,
            @RequestParam(name = "beginTime", required = false) String beginTime,
            @RequestParam(name = "endTime", required = false) String endTime) {

        return ordersService.page(pageNum, pageSize, number, beginTime, endTime);
    }

    /**
     * 派送
     *
     * @param orderDto
     * @return
     */
    @PutMapping
    public R updateStatus(@RequestBody OrderDto orderDto) {
        //获取请求的参数
        Integer status = orderDto.getStatus();
        Long id = orderDto.getId();
        //核心业务
        Orders orders = new Orders();
        orders.setId(id);
        orders.setStatus(status);
        boolean result = ordersService.updateById(orders);
        if (result) {
            return R.success("派送成功", result);
        }
        return R.error("派送失败");

    }

    /**
     * 前台客户 分页查询历史订单
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R userPage(
            @RequestParam(name = "page", required = false, defaultValue = "1") Long pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "1") Long pageSize
    ) {
        //获取当前用户
        Long userId = ThreadLocalUtil.getCurrentId();
        //查询当前用户的订单信息
        Page<Orders> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(userId != null, Orders::getUserId, userId)
                .orderByDesc(Orders::getOrderTime);
        ordersService.page(page, lqw);
        List<Orders> ordersList = page.getRecords();

        List<OrdersVo> ordersVoList = ordersList.stream().map(orders -> {
            OrdersVo ordersVo = new OrdersVo();
            BeanUtils.copyProperties(orders, ordersVo);
            //查询订单明细
            List<OrderDetail> orderDetailList = orderDetailService.list(Wrappers.lambdaQuery(OrderDetail.class)
                    .eq(OrderDetail::getOrderId, orders.getNumber()));
            ordersVo.setOrderDetails(orderDetailList);
            return ordersVo;
        }).collect(Collectors.toList());

        Page<OrdersVo> vo = new Page<>();
        BeanUtils.copyProperties(page, vo, "records");
        vo.setRecords(ordersVoList);
        return R.success("查询成功", vo);
    }

    @PostMapping("/again")
    public R again(@RequestBody Orders orders) {
        return ordersService.again(orders);
    }
}
